package ipebg.mzk.common.util;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class StringTool {
	
	
	public static int SubDay(Date to,Date from){

	    long to1 = to.getTime();

	    long from1 = from.getTime();

	    return  (int) ((to1 - from1) / (1000 * 60 * 60 * 24));
	}

	/**
	 * Get system Date of string format
	 * 
	 * @param dateFormat
	 *            if "" or null then "yyyy/MM/dd HH:mm:ss"
	 * @return
	 */
	public static String GetCurrentSystemDate(String dateFormat) {
		String df = dateFormat, strNow;
		
		Date now = new Date();
		if (df == null || df.equals("")) {
			df = "yyyy/MM/dd HH:mm:ss";
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(df);
			strNow = sdf.format(now);
		} catch (Exception e) {
			strNow = "";
		}
		return strNow;
	}

	@SuppressWarnings("deprecation")
	/**
	 * String convert to Date
	 * @param strDate
	 * @param dateFormat
	 * @return
	 */
	public static Date ConvertString2Date(String strDate,String dateFormat) {
		String df = dateFormat;
		java.util.Date time = null;
		if (df == null || df.equals("")) {
			df = "yyyy/MM/dd";
		}
		
			//System.out.println(strDate);
			SimpleDateFormat sdf = new SimpleDateFormat(df);
			try {
				time = sdf.parse(strDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		return time;
	}
	
	/**
	 * 将字符集串格式化为sql查询格式： 1 2 3 ==> '1','2','3'
	 * 
	 * @param str
	 * @return
	 */
	public static String SqlFormatConvert(List<String> str) {
		String strReturn = "";
		if (str != null && str.size() > 0) {
			for (String s : str) {
				strReturn += "'" + s + "',";
			}
			strReturn = strReturn.substring(0, strReturn.length() - 1);
		}
		return strReturn;
	}
	
	
	/**
	 * 将字符集串格式化为sql查询格式： 1;2;3; ==> '1','2','3'
	 */
	public static String StringToString(String str){
		String list="";
		List<String> sList=new ArrayList<String>();
		String[] zList=str.split(";");
		for(int i=0;i<zList.length;i++){
			sList.add(zList[i]);
		}
		list= SqlFormatConvert(sList);
		return list;
	}
	
	
	public static String join(List<String> list, String str) {
		if(list == null || list.isEmpty()) return "";
		StringBuffer buf = new StringBuffer("");
		for (int i = 0; i < list.size(); i++) {
			if(i != 0) buf.append(str);
			buf.append(list.get(i)); 
		}
		return buf.toString();
	}
	
	/**
	 * 判断一个字体是否为空
	 * @param str
	 * @return 为空返回true,否则返回false
	 */
	public static boolean isEmpty(String str){
		if(str==null||str.trim().equals("")){
			return true;
		}
		return false;
		
	}

	
	public static void main(String args[]){
		System.out.println(GetCurrentSystemDate("yyyy_MM_dd"));
		//System.out.println(String2Long("F3219057"));
		//System.out.print(Long2String(705051525354555657L));
	}
	
	
	public static String nullToStr(String str)
	{
		if (str == null||str.equals("null"))
		{
			return "";
		}
		else
		{
			return str;
		}
	}

	public static String nullToStr(Object str) {
		String s = "";
		if (str != null) {
			s = (str.toString()).trim();
		} else {
			s = "";
		}
		return s;
	}

	public static String utfToBig5(String str) {
		String s = null;
		try {
			s = new String(str.trim().getBytes("UTF-8"), "Big5");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
	}
	
	public static String iso8859ToBig5(String str) {
		String s = null;
		try {
			s = new String(str.trim().getBytes("ISO-8859-1"), "Big5");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
	}
	
	public static String gb2312ToBig5(String str) {
		String s = null;
		try {
			s = new String(str.trim().getBytes("GB2312"), "Big5");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
	}
	
}
